//5
//5 15 40 30 10
// 生成哈夫曼树, 并中序遍历输出,  同样的权值, 右子树更高
const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

class TreeNode {
    constructor(value, left = null, right = null, height = 0) {
        this.value = value
        this.left = left
        this.right = right
        this.height = 0
    }
}



let n;
let initArr = [];
rl.on("line", (line)=>{
    if(n === undefined){
        n = Number(line.trim())
    } else {
        initArr = line.trim().split(/\s+/).map(Number)
        console.log(getResult(n, initArr))
        // clear
        n = undefined
        initArr.length = 0
    }

})

function getResult(n, initArr) {
    initArr = initArr.map((value, index) => new TreeNode(value))
    sort(initArr)
    if(initArr.length === 1) return initArr[0].value // 一个元素

    while(initArr.length > 1) {
        let left = initArr.shift()
        let right = initArr.shift()
        let root = new TreeNode(left.value + right.value, left, right, Math.max(left.height, right.height) + 1)
       if(left.value === right.value && left.height > right.height) {
           root.left = right
           root.right = left
       }
        initArr.push(root)
        sort(initArr)
    }

    // 中序遍历
    let ans = []
    midTraversal(initArr[0], ans)
    return ans.join(" ")
}

function midTraversal(root, ans) {
    if(root.left) {
        midTraversal(root.left, ans)
    }
    ans.push(root.value)
    if(root.right){
        midTraversal(root.right, ans)
    }
}

function sort(initArr) {
    initArr.sort((a, b) => a.value - b.value)
}